#include "point.h"
#include <iostream>
#include <cmath>
#include <cstdlib>

point_t::point_t() : x(.0), y(.0) 
{
}

point_t::point_t(double _x, double _y) : x(_x), y(_y)
{
}

point_t & point_t::operator+=(point_t const & right)
{
    x += right.x;
    y += right.y;
    return *this;
}

point_t & point_t::operator-=(point_t const & right)
{
    x -= right.x;
    y -= right.y;
    return *this;
}

point_t & point_t::operator*=(double num)
{
    x *= num;
    y *= num;
    return *this;
}

point_t & point_t::operator/=(double num)
{
    x /= num;
    y /= num;
    return *this;
}

bool operator==(point_t const & left, point_t const & right)
{
    return (left.x == right.x && left.y == right.y);
}

bool operator!=(point_t const & left, point_t const & right)
{
    return !(left == right);
}

point_t operator+(point_t right, point_t const & left)
{
    return (right += left);
}

point_t operator-(point_t right, point_t const & left)
{
    return (right -= left);
}

point_t operator*(point_t point, double num)
{
    return (point *= num);
}

point_t operator*(double num, point_t point)
{
    return (point *= num);
}

point_t operator/(point_t point, double num)
{
    return (point /= num);
}

double distance(point_t const & a, point_t const & b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

std::ostream & operator<<(std::ostream & out, point_t const & point)
{
    out << '(' << point.x << ", " << point.y << ')';
    return out;
}

std::istream & operator>>(std::istream & in, point_t & point)
{
    double x = 0;
    double y = 0;
    char * delimiters = new char[3];
    in >> delimiters[0] >> x >> delimiters[1] >> y >> delimiters[2];
    if (delimiters[0] == '(' && delimiters[1] == ',' && delimiters[2] == ')')
        point = point_t(x, y);
    delete[] delimiters;
    return in;
}
